package com.ryskoo.controller.promotion;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ryskoo.catering.coupon.domain.CouponPO;
import com.ryskoo.customer.vendordiscounts.domain.VendorDiscountsPO;
import com.ryskoo.customer.vendordiscounts.service.IVendorDiscountsService;
import com.ryskoo.framework.core.bean.ReturnCodeEnum;
import com.ryskoo.framework.core.bean.ReturnMessageDTO;
import com.ryskoo.framework.core.controller.BasicHandler;
import com.ryskoo.framework.core.exception.InvalidParamException;
import com.ryskoo.framework.core.utils.LoginUtil;

@Controller("discountController")
@RequestMapping("/vendor/cater/discounts")
public class VendorDiscountsController extends BasicHandler<CouponPO> {

    private static final String TEMPLATE_PATH = "system/promotion";

    private static final String MODULE_NAME = "Discounts";
    /**
     * 基类构造方法.
     */
    public VendorDiscountsController() {
        super(TEMPLATE_PATH, MODULE_NAME);
    }

    private IVendorDiscountsService vendorDiscountsService;

    
    /**
     * 消费者注入
     *
     * @param productService 消费者基类
     */
    @Autowired
    protected void setService(IVendorDiscountsService vendorDiscountsService) {
        this.vendorDiscountsService = vendorDiscountsService;
        super.setService(vendorDiscountsService);
    }
    
    /**
     * 根据商户uuid查询优惠设置
     * @param vendorUuid
     * @return
     */
    @GetMapping("/todiscounts")
    public String toDiscounts(Model model, HttpServletRequest request) {
        String vendorUuid = LoginUtil.getLoginVendorUuid();
        VendorDiscountsPO po = vendorDiscountsService.getByVendorUuid(vendorUuid);
        model.addAttribute("dto", po);
        return TEMPLATE_PATH + "/" + MODULE_NAME + "Add";
    }
    

    /**
     * 保存优惠设置
     * @param body
     * @return
     */
    @PostMapping("/save")
    @ResponseBody
    public ReturnMessageDTO save(@RequestBody VendorDiscountsPO po) {
        ReturnMessageDTO returnMessage = new ReturnMessageDTO();
        try {
            vendorDiscountsService.save(po);
            returnMessage.setReturnCode(ReturnCodeEnum.SUCCESS.getReturnCode());
            returnMessage.setReturnNote("保存成功");
        } catch (InvalidParamException e) {
            returnMessage.setReturnCode(ReturnCodeEnum.FAIL.getReturnCode());
            returnMessage.setReturnNote(e.getMessage());
        } catch (Exception e) {
            returnMessage.setReturnCode(ReturnCodeEnum.ERROR.getReturnCode());
            returnMessage.setReturnNote("保存失败");
        }
        return returnMessage;
    }

}
